package 锁;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/5/6 13:57
 * @description:
 * @version:
 */
public class TestSyn {
    public static void main(String[] args) {
        Number number = new Number();

        new Thread(new Runnable() {
            @Override
            public void run() {
//                number.getOne();
                Number.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                number.getTwo();
//                Number.getTwo();
            }
        }).start();
    }
}

class Number{
    //.Synchronized修饰静态方法，实际上是对该类对象加锁，俗称“类锁”
    public static synchronized void getOne(){
        try {
            Thread.sleep(2000);
            System.out.println("one");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //Synchronized修饰非静态方法，实际上是对调用该方法的对象加锁，俗称“对象锁”(类锁和对象锁的访问是不冲突的)
    public synchronized  void getTwo(){
        System.out.println("two");
    }
}